<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: db.class.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
defined('IN_TWCMS') || exit('Access Denied');

class DB{
	var $querynum = 0;
	var $link;
	var $histories;
	var $time;
	var $dbpre;

	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = 'latin1', $pconnect = 0, $dbpre='', $time = 0) {
		$this->time = $time;
		$this->dbpre = $dbpre;

		$func = $pconnect ? 'mysql_pconnect' : 'mysql_connect';
		if(!$this->link = $func($dbhost, $dbuser, $dbpw)) return 'error_connect';

		if($this->version() > '4.1') {
			if($dbcharset) {
				$dbcharset=str_replace('-','',$dbcharset);
				mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link);
			}

			if($this->version() > '5.0.1') {
				mysql_query("SET sql_mode=''", $this->link);
			}
		}

		if($dbname) {
			if(!mysql_select_db($dbname, $this->link)) return 'error_select';
		}

		return 'ok';
	}

	function insert($table,$arr) {
		$k=$v='';
		foreach($arr as $kay=>$val) {
			$k.=$this->S($kay, '`').',';
			$v.=$this->S($val).',';
		}
		$sql = 'INSERT INTO `'.TW_DBPRE.$table.'` ('.rtrim($k,',').') VALUES ('.rtrim($v,',').')';
		return $this->query($sql);
	}
	
	function update($table,$arr,$where) {
		$k='';
		foreach($arr as $kay=>$val) {
			$k.=$this->S($kay, '`').'='.$this->S($val).',';
		}
		return $this->query('UPDATE `'.TW_DBPRE.$table.'` SET '.rtrim($k,',').' WHERE '.$where);
	}

	function S($str, $quot="'") {
		return is_numeric($str) ? $str : $quot.mysql_real_escape_string($str).$quot;
	}

	function select_db($dbname) {
		return mysql_select_db($dbname, $this->link);
	}

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function fetch_object($query) {
		return mysql_fetch_object($query);
	}

	function result_first($sql) {
		$query = $this->query($sql);
		return $this->result($query, 0);
	}

	function fetch_first($sql) {
		$query = $this->query($sql);
		return $this->fetch_array($query);
	}

	function fetch_all($sql) {
		$arr = array();
		$query = $this->query($sql);
		while($data = $this->fetch_array($query)) {
			$arr[] = $data;
		}
		return $arr;
	}

	function fetch_all_row($sql) {
		$arr = array();
		$query = $this->query($sql);
		while($data = $this->fetch_row($query)) {
			$arr[] = $data;
		}
		return $arr;
	}

	function query($sql, $silent = 1, $unbuff = 0) {
		$func = $unbuff && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql, $this->link)) && $silent) {
			$this->halt($sql);
		}
		$this->querynum++;
		$this->histories[] = $sql;
		return $query;
	}

	function affected_rows() {
		return mysql_affected_rows($this->link);
	}

	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}

	function result($query, $row = 0) {
		return @mysql_result($query, $row);
	}

	function num_rows($query) {
		return mysql_num_rows($query);
	}

	function num_fields($query) {
		return mysql_num_fields($query);
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	function fetch_row($query) {
		return mysql_fetch_row($query);
	}

	function fetch_fields($query) {
		return mysql_fetch_field($query);
	}

	function version() {
		return mysql_get_server_info($this->link);
	}

	function close() {
		return mysql_close($this->link);
	}

	function halt($sql) {
		defined('TW_DBPRE') && $sql = str_replace(TW_DBPRE, '***', $sql);
		echo '<b>ERROR : </b>'.$this->error().'<br><b>ERRNO : </b>'.$this->errno().'<br><b>QUERY : </b>'.$sql;	//MySQL Query Error
	}
}